<?php

namespace Admin\Controller;

class OrderController extends AdminController {

    public function index() {
        $where = '1=1 ';
        if (IS_CHEDUI) {
            $fleet_id = M('Fleet')->where(['uid' => UID])->getField('fleet_id');
            $where .= 'and o.fleet_id= ' . $fleet_id;
        }

        $order_no = I("order_no");

        if (!empty($order_no)) {
            $where .= " and o.order_no =" . $order_no;
            $this->assign('order_no', $order_no);
        }

        $phone = I("phone");
        if (!empty($phone)) {
            $where .= " and c.company_phone =" . $phone;
            $this->assign('phone', $phone);
        }
        $pick_no = I("pick_no");
        if (!empty($pick_no)) {
            $where .= " and o.pick_no =" . $pick_no;
            $this->assign('pick_no', $pick_no);
        }
        $company_name = I("company_name");
        if (!empty($company_name)) {
            $where .= " and c.company_name like '%" . $company_name . "%'";
            $this->assign('company_name', $company_name);
        }

        $status = I("status", -1);

        if ($status != -1) {
            $where .= " and o.order_status =" . $status;
            $this->assign('status', $status);
        } else {
            $where .= " and o.order_status <> 0 and  o.order_status <> 60";
        }

        $is_export = I('is_export', 0, 'intval');

        //日期选择
        $start_date = I("start_date");
        $end_date = I("end_date");



        if (!empty($start_date)) {
//                $where['sp.create_time'] = array('EGT', $start_date);
            $where .= " and o.shipment_time >= '$start_date 00:00:00' and o.shipment_time <= '$start_date 23:59:59'";
            $this->assign("start_date", $start_date);
        }

        if (!empty($end_date)) {
//                $where['sp.create_time'] = array('ELT', $end_date . ' 23:59:59');
            $where .= " and o.cutoff_time >= '$start_date 00:00:00' and o.cutoff_time <= '$start_date 23:59:59'";
            $this->assign("end_date", $end_date);
        }

        $box['small_carpool'] = '小柜拼车';
        $box['small_single'] = '小柜单放';
        $box['big_cabinet'] = '大柜';
        $box['tall_cabinet'] = '高柜';
        $box['super_tall_cabinet'] = '超高柜';

        $status_map[0] = '待支付';
        $status_map[10] = '派单中';
        $status_map[20] = '待司机接单';
        $status_map[30] = '运输中';
        $status_map[31] = '待审核额外费用';
        $status_map[32] = '额外费用已拒绝';
        $status_map[40] = '已到港';
        $status_map[50] = '已完成';
        $status_map[60] = '已取消';


        $M = M()->table("ot_order o")
                ->join('ot_company c on o.company_id = c.company_id')
                ->join('ot_shipment_address sa on o.shipment_address_id = sa.shipment_address_id')
                ->join('left join ot_dock_weight dw on o.weight_id=dw.weight_id')
                ->join('left join ot_driver dr ON dr.driver_id=o.driver_id')
                ->join('left join ot_fleet f ON f.fleet_id=o.fleet_id')
        ;

        $field = 'o.*,c.company_name,sa.province,sa.city,sa.address,sa.address_desc,dw.weight_desc,f.fleet_name';

        $order = "o.order_id desc";

        if ($is_export) {
            $exportList = $M->field($field)->where($where)->order($order)->select();
            ExcelOrder($exportList, IS_CHEDUI);
            die;
        }


        $list = $this->lists($M, $where, $order, $field);

        $fleetlist = M('Fleet')->field('fleet_id,fleet_name')->where(['fleet_status' => 1])->select();

        $this->assign('fleetlist', $fleetlist);
        $this->assign('status_map', $status_map);
        $this->assign('box', $box);
        $this->assign('_list', $list);
        $this->display();
    }

    public function getRecmdDriver() {
        $id = I('id', 0, 'intval');
        $chedui_id = I('chedui_id', -1, 'intval');

        $driver_id = M('Order')->where(['order_id' => $id])->getField('recmd_driver_id');

        $info = M()->table('ot_driver as d')
                ->field('f.fleet_name,d.driver_name')
                ->join('ot_fleet as f on d.fleet_id = f.fleet_id')
                ->where(['driver_id' => $driver_id])
                ->find();

        if ($chedui_id != -1) {
            $driverList = M('Driver')->field('driver_id,driver_name')->where(['fleet_id' => $chedui_id, 'status' => 1, 'user_status' => 0])->select();
            $res['driver'] = $driverList;
        }

        $res['data'] = $info['fleet_name'] . '--' . $info['driver_name'];
        $this->ajaxReturn($res, 'JSON');
    }

    /**
     * 后台派发司机
     */
    public function allotDriver() {
        $order_id = I('id', 0, 'intval');
        $driver_id = I('siji_id', 0, 'intval');
        if ($order_id == 0) {
            $this->error('错误的参数');
        }

        if ($driver_id == 0) {
            $this->error('错误的参数');
        }
        $data['driver_id'] = $driver_id;
        $data['order_status'] = 20; //20-运输中
        $res = M('Order')->where(['order_id' => $order_id, 'order_status' => 10])->save($data);
        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

    /**
     * 后台派发车队
     */
    public function allotFleet() {
        $order_id = I('id', 0, 'intval');
        $fleet_id = I('chedui_id', 0, 'intval');
        if ($order_id == 0) {
            $this->error('错误的参数');
        }

        if ($fleet_id == 0) {
            $this->error('错误的参数');
        }
        $data['fleet_id'] = $fleet_id;
        $res = M('Order')->where(['order_id' => $order_id, 'order_status' => 10])->save($data);
        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

    public function info() {
        $id = I('id', 0, 'intval');

        $data = M()
                ->table('ot_order o')
                ->field('o.*,c.company_name,cu.company_user_name,cu.company_user_phone,ba.box_linkman,ba.box_linkman_phone,ba.box_address_desc,f.fleet_name,d.driver_name,d.driver_phone,d.car_no,p.port_name')
                ->join('left join ot_company c on o.company_id = c.company_id')
                ->join('left join ot_company_user cu on o.company_user_id = cu.company_user_id')
                ->join('left join ot_box_address ba on o.box_address_id = ba.box_address_id')
                ->join('left join ot_fleet f on o.fleet_id = f.fleet_id')
                ->join('left join ot_driver d on o.driver_id = d.driver_id')
                ->join('left join ot_dock do on o.dock_id = do.dock_id')
                ->join('left join ot_port p on do.port_id = p.port_id')
                ->where(['o.order_id' => $id])
                ->find();

        if (!empty($data)) {
            $data['box_img'] = json_decode($data['box_img']);

            $shipment_address_id = explode(',', $data['shipment_address_id']);

            $shipment_address = M()
                    ->table('ot_shipment_address as sa')
                    ->field('sa.*,b.boxman_name,b.boxman_phone')
                    ->join('ot_boxman as b on sa.boxman_id = b.boxman_id')
                    ->where(['shipment_address_id' => array('in', $shipment_address_id)])
                    ->select();

            $data['shipment_info'] = $shipment_address;
        }

        $box['small_carpool'] = '小柜拼车';
        $box['small_single'] = '小柜单放';
        $box['big_cabinet'] = '大柜';
        $box['tall_cabinet'] = '高柜';
        $box['super_tall_cabinet'] = '超高柜';

        $fleetlist = M('Fleet')->field('fleet_id,fleet_name')->where(['fleet_status' => 1])->select();

        $this->assign('fleetlist', $fleetlist);
        $this->assign('box', $box);
        $this->assign('data', $data);
        $this->display();
    }

    public function otherPrice() {
        $id = I('id', 0, 'intval');
        $orderInfo = M()->table('ot_order o')->field('o.*,d.dock_name,p.port_name,dw.weight_desc,dw.weight_price')
                ->join('LEFT JOIN ot_dock as d ON d.dock_id = o.dock_id')
                ->join('LEFT JOIN ot_port as p ON p.port_id = o.port_id')
                ->join('LEFT JOIN ot_dock_weight as dw ON dw.weight_id = o.weight_id')
                ->where(['o.order_id' => $id])
                ->find();
        if (!empty($orderInfo['other_price_img'])) {
            $arr_other_price_img = json_decode($orderInfo['other_price_img'], TRUE);
            foreach ($arr_other_price_img as &$val) {
                $val = C('SITE_URL') . $val;
            }
            unset($val);
            $orderInfo['other_price_img'] = $arr_other_price_img;
        }
        $this->assign('order_id', $id);
        $this->assign('data', $orderInfo);
        $this->display();
    }

    public function jujueOtherPrice() {
        $id = I('id', 0, 'intval');
        $data['order_status'] = 32;
        $data['other_pay_refuse'] = I('refund_desc');

        M('Order')->where(['order_id' => $id])->save($data);
        $this->success('操作成功', U('info', array('id' => $id)));
    }

    public function tongguoOtherPrice() {
        $id = I('id', 0, 'intval');

        $data['order_status'] = 40;
        $data['admin_price'] = I('admin_price', 0);
        M('Order')->where(['order_id' => $id])->save($data);
        $this->success('操作成功', U('info', array('id' => $id)));
    }

    public function setFengOrXiang() {
        $id = I('id', 0, 'intval');
        $s = I('s', 1, 'intval');
        $no = I('no');

        if ($id == 0) {
            $this->error('请选择正确的订单');
        }

        $no_name = '箱号';
        $data['box_no'] = $no;

        if ($s == 2) {
            $no_name = '封号';
            $data['close_no'] = $no;
        }

        if (empty($no)) {
            $this->error('请填写' . $no_name);
        }

        $res = M('Order')->where(['order_id' => $id])->save($data);

        if ($res) {
            $this->success('修改成功!');
        } else {
            $this->error('修改失败!');
        }
    }

}
